﻿//-----------------------------------------------------------------------
// <copyright file="Favourites.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views
{
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Favourites : Page
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Favourites"/> class.
        /// </summary>
        public Favourites()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Navigates to the selected project.
        /// </summary>
        /// <param name="sender">The sending listview</param>
        private void NavigateToProject(object sender)
        {
            var senderList = sender as ListView;
            var selectedProject = senderList.SelectedItem as Models.Project;
            var viewModel = this.DataContext as ViewModels.Favourites;

            if (selectedProject == null || viewModel == null)
            {
                return;
            }

            viewModel.NavigateToSelectedProject(selectedProject);
        }

        /// <summary>
        /// Raised when the listview is tapped
        /// </summary>
        /// <param name="sender">The list view.</param>
        /// <param name="e">The arguments for the tap</param>
        private void ListView_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            this.NavigateToProject(sender);    
        }

        /// <summary>
        /// Raised when the key is pressed.
        /// </summary>
        /// <param name="sender">The list view.</param>
        /// <param name="e">The arguments for the key</param>
        private void ListView_KeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                this.NavigateToProject(sender);

                e.Handled = true;
            }
        }
    }
}
